package com.kelvem.saas.workbanch.core.system.controller;

import com.kelvem.saas.workbanch.core.base.Page;
import com.kelvem.saas.workbanch.core.exception.SaasAssert;
import com.kelvem.saas.workbanch.security.shiro.ShiroUserService;
import com.kelvem.saas.workbanch.core.system.model.SysUserEntity;
import com.kelvem.saas.workbanch.core.system.request.SysUserQueryVo;
import com.kelvem.saas.workbanch.core.system.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户表控制层
 *
 * @author kelvem
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private ShiroUserService shiroUserService;

    
    /**
     * 增加用户表
     * curl -X POST http://localhost:9090/sysUser/v1/add -H 'Content-Type: application/json' -d '{"id": null, "username": "username", "password": "password", "email": "email", "phone": "phone", "displayName": "display_name", "status": "status", "lastLoginIp": "last_login_ip", "lastLoginTime": "2024-01-01 01:02:03", "avatar": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", "birthday": "2024-01-01 01:02:03", "sex": 5, "adminFlag": 1, "sourceType": 1, "delFlag": 0, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo", "tenantId": 7}'
     *
     * @param sysUser 用户表信息
     * @return Long 主键id
     */
    @PostMapping("/v1/add")
    public Long add(@RequestBody(required = false) SysUserEntity sysUser) {
        SaasAssert.notNull(sysUser);

        log.debug("SysUserController:add:start, sysUser=【{}】", sysUser);
        Long sysUserId = sysUserService.addSysUser(sysUser);
        log.debug("SysUserController:add:end, sysUserId={}", sysUserId);

        return sysUserId;
    }

    /**
     * 修改用户表
     * curl -X PUT http://localhost:9090/sysUser/v1/update -H 'Content-Type: application/json' -d '{"id": 10000, "username": "username", "password": "password", "email": "email", "phone": "phone", "displayName": "display_name", "status": "status", "lastLoginIp": "last_login_ip", "lastLoginTime": "2024-01-01 01:02:03", "avatar": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", "birthday": "2024-01-01 01:02:03", "sex": 5, "adminFlag": 1, "sourceType": 1, "delFlag": 0, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo", "tenantId": 7}'
     *
     * @param sysUser 用户表信息
     * @return Integer 修改记录数
     */
    @PostMapping("/v1/update")
    public Integer update(@RequestBody(required = false) SysUserEntity sysUser) {
        SaasAssert.notNull(sysUser);

        log.debug("SysUserController:update:start, sysUser=【{}】", sysUser);
        int count = sysUserService.updateSysUser(sysUser);
        log.debug("SysUserController:update:end, count={}", count);

        return count;
    }

    /**
     * 删除用户表
     * curl -X DELETE 'http://localhost:9090/sysUser/v1/delete/10002'
     *
     * @param sysUserId 用户表id
     * @return Integer 删除记录数
     */
    @PostMapping("/v1/delete/{sysUserId}")
    public Integer delete(@PathVariable(required = false) Long sysUserId) {
        SaasAssert.notNull(sysUserId);

        log.debug("SysUserController:delete:start, sysUserId={}", sysUserId);
        int count = sysUserService.deleteSysUser(sysUserId);
        log.debug("SysUserController:delete:end, count={}", count);

        return count;
    }

    /**
     * 查询用户表分页数据
     * curl -X POST http://localhost:9090/sysUser/v1/queryPage -H 'Content-Type: application/json' -d '{"id": 10000}'
     *
     * @param sysUserQueryVo 查询用
     * @return Page<SysUserEntity> 用户表分页数据
     */
    @PostMapping("/v1/queryPage")
    public Page<SysUserEntity> queryPage(@RequestBody(required = false) SysUserQueryVo sysUserQueryVo) {
        SaasAssert.notNull(sysUserQueryVo);

        log.debug("SysUserController:queryPage:start, sysUserQueryVo=【{}】", sysUserQueryVo);
        Page<SysUserEntity> page = sysUserService.querySysUserPage(sysUserQueryVo);
        log.debug("SysUserController:queryPage:end, page.count={}", page.getSize());

        return page;
    }

    /**
     * 根据id查用户表信息
     * curl -X GET 'http://localhost:9090/sysUser/v1/get/10000'
     *
     * @param sysUserId 用户表id
     * @return SysUser 用户表信息
     */
    @GetMapping("/v1/get/{sysUserId}")
    public SysUserEntity getById(@PathVariable(required = false) Long sysUserId) {
        SaasAssert.notNull(sysUserId);

        log.debug("SysUserController:getById:start, sysUserId={}", sysUserId);
        SysUserEntity sysUser = sysUserService.getSysUserById(sysUserId);
        log.debug("SysUserController:getById:end, sysUser=【{}】", sysUser);

        return sysUser;
    }

    /**
     * 注册用户
     * curl -X POST http://localhost:9090/sysUser/v1/register -H 'Content-Type: application/json' -d '{"username": "username", "password": "password", "email": "email", "phone": "phone", "displayName": "display_name", "status": "status", "lastLoginIp": "last_login_ip", "lastLoginTime": "2024-01-01 01:02:03", "avatar": ""https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif, "birthday": "2024-01-01 01:02:03", "sex": 5, "adminFlag": 1, "sourceType": 1, "delFlag": 0, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo", "tenantId": 7}'
     *
     * @param sysUser 用户信息
     * @return 用户id
     */
    @PostMapping("/v1/register")
    public Long register(@RequestBody SysUserEntity sysUser) {
        Long id = sysUserService.addSysUser(sysUser);
        return id;
    }
    
    /**
     * 登录用户
     * curl -X POST http://localhost:9090/sysUser/v1/login -H 'Content-Type: application/json' -d '{"username": "username", "password": "password"}'
     *
     * @param sysUser 用户名&密码
     * @return token
     */
    @PostMapping("/v1/login")
    public String login(@RequestBody SysUserEntity sysUser) {
        SaasAssert.notNull(sysUser);
        SaasAssert.notNull(sysUser.getUsername());
        SaasAssert.notNull(sysUser.getPassword());

        String username = sysUser.getUsername();
        String password = sysUser.getPassword();
        String jwtToken = this.shiroUserService.login(username, password);

        return jwtToken;
    }

    /**
     * 根据token查用户信息
     * curl -X GET 'http://localhost:9090/sysUser/v1/get/10000'
     *
     * @param token 令牌
     * @return SysUser 用户表信息
     */
    @GetMapping("/v1/getByToken")
    public SysUserEntity getByToken(@RequestParam(required = false) String token) {
        SaasAssert.notNull(token);

        log.debug("SysUserController:getByToken:start, token={}", token);
        SysUserEntity sysUser = this.shiroUserService.getUserByToken(token);
        log.debug("SysUserController:getByToken:end, token=【{}】", token);

        return sysUser;
    }

    /**
     * 为组织鉴权
     * curl -X POST 'http://localhost:9090/sysUser/v1/authorizeOrgMenus'
     *
     * @param orgId 组织id
     * @param bizPageIdList 菜单ids
     * @return 结果
     */
    @PostMapping("/v1/authorizeOrgMenus")
    public Integer authorizeOrgMenus(@RequestParam(required = false) Long orgId, @RequestParam(required = false) List<Long> bizPageIdList) {
        SaasAssert.notNull(orgId);

        log.debug("SysUserController:getByToken:start, orgId={}", orgId);
        Integer cnt = this.shiroUserService.authorizeOrgMenus(orgId, bizPageIdList);
        log.debug("SysUserController:getByToken:end, cnt=【{}】", cnt);

        return cnt;
    }
    
}
